/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import com.yami.shop.bean.event.PaySuccessOrderEvent;
import com.yami.shop.bean.event.RefundSuccessOrderEvent;
import com.yami.shop.payment.WeChatPayment;
import com.yami.shop.payment.dto.GetJsapiServiceDto;
import com.yami.shop.payment.event.PaymentEvent;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lanhai
 */
@Hidden
@RestController
@RequestMapping("/wxNotice")
@AllArgsConstructor
public class PayNoticeController {
//模拟支付不需要回调
//    /**
//     * 小程序支付
//     */
//    private final WxPayService wxMiniPayService;
//
//    private final PayService payService;
//
//
//    @RequestMapping("/order")
//    public ServerResponseEntity<Void> submit(@RequestBody String xmlData) throws WxPayException {
//        WxPayOrderNotifyResult parseOrderNotifyResult = wxMiniPayService.parseOrderNotifyResult(xmlData);
//
//        String payNo = parseOrderNotifyResult.getOutTradeNo();
//        String bizPayNo = parseOrderNotifyResult.getTransactionId();
//
//        // 根据内部订单号更新order settlement
//        payService.paySuccess(payNo, bizPayNo);
//
//
//        return ServerResponseEntity.success();
//    }


    private static final Logger log = LoggerFactory.getLogger(PayNoticeController.class);
    @Autowired
    private WeChatPayment weChatPayment;

    @Autowired
    private ApplicationEventPublisher publisher;


    /**
     * 微信支付/充值回调
     */
    @PostMapping("/payment/notify")
    public Map<String, String>  renotify(HttpServletRequest request) {

        GetJsapiServiceDto getJsapiServiceDto = new GetJsapiServiceDto();
        getJsapiServiceDto.setWechatpayMchid(WeChatPayment.wechatpayMchid);
        getJsapiServiceDto.setWechatpayMchserialno(WeChatPayment.wechatpayMchserialno);
        getJsapiServiceDto.setWechatpayPrivatekey(WeChatPayment.wechatpayPrivatekey);
        getJsapiServiceDto.setWechatpayApiv3key(WeChatPayment.wechatpayApiv3key);

        getJsapiServiceDto.setWechatpayAppid(WeChatPayment.wechatpayAppid);
        getJsapiServiceDto.setWechatpayAppsecret(WeChatPayment.wechatpayAppsecret);

        Map map = weChatPayment.paySuccessCheck(request,getJsapiServiceDto);

        Integer amount = (Integer)map.get("amount");
        String outTradeNo = (String)map.get("outTradeNo");

        String attach = (String)map.get("attach");
        //Map<String, Object> map1 = new HashMap<>();
        //将附加信息 attach 解析到 map1 中,因为现在没有传递任何参数,所以没有解析

        BigDecimal divide = new BigDecimal(amount).divide(new BigDecimal("100"));

        //publisher.publishEvent(new PaymentEvent(this, outTradeNo,0,attach,divide.toString()));

        publisher.publishEvent(new PaySuccessOrderEvent(outTradeNo,""));

        Map<String, String> res = new HashMap<>();
        res.put("code", "SUCCESS");
        res.put("message", "成功");
        return res;
    }

    /**
     * 微信退款回调
     */
    @PostMapping("/refund/notify")
    public Map<String, String> parseRefundNotifyResult(HttpServletRequest request) {
        GetJsapiServiceDto getJsapiServiceDto = new GetJsapiServiceDto();
        getJsapiServiceDto.setWechatpayMchid(WeChatPayment.wechatpayMchid);
        getJsapiServiceDto.setWechatpayMchserialno(WeChatPayment.wechatpayMchserialno);
        getJsapiServiceDto.setWechatpayPrivatekey(WeChatPayment.wechatpayPrivatekey);
        getJsapiServiceDto.setWechatpayApiv3key(WeChatPayment.wechatpayApiv3key);

        getJsapiServiceDto.setWechatpayAppid(WeChatPayment.wechatpayAppid);
        getJsapiServiceDto.setWechatpayAppsecret(WeChatPayment.wechatpayAppsecret);

        Map map = weChatPayment.refundSuccessCheck(request,getJsapiServiceDto);

        Long amount = (Long)map.get("amount");
        String outTradeNo = (String)map.get("outTradeNo");
        String outRefundNo = (String)map.get("outRefundNo");

        BigDecimal divide = new BigDecimal(amount).divide(new BigDecimal("100"));

        //publisher.publishEvent(new PaymentEvent(this, outTradeNo,1,null,divide.toString()));
        publisher.publishEvent(new RefundSuccessOrderEvent(outTradeNo,outRefundNo,divide));

        Map<String, String> res = new HashMap<>();
        res.put("code", "SUCCESS");
        res.put("message", "成功");
        return res;
    }
}
